﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DataService;

public partial class AccountInfomation_Default : BasePage
{
    private CustomerService _customerService = new CustomerService();
    private Customer _customer;
    protected void Page_Load(object sender, EventArgs e)
    {
        Initial();        
    }

    private void Initial()
    {
        if (UserLogin != null)
        {
            if (!UserLogin.IsAdmin)
            {
                hlMainContactInfomation.Visible = false;
                hlEditPassphrase.Visible = false;
            }
            _customer = UserLogin.Customer;
            ShowInfo(UserLogin.Customer);
        }
    }

    private void ShowInfo(Customer customer)
    {
        if (customer != null)
        {
            LiteralMainContact.Text += customer.CompanyName + "<br />";
            LiteralMainContact.Text += customer.MainContact.ToString();
            if (customer.Contacts.Count > 0)
            {
                dtListContacts.DataSource = customer.Contacts;
                dtListContacts.DataBind();
            }
        }  
        if (UserLogin != null)
        {
            lblUserCurrent.Text = UserLogin.Username;
            LiteralYourContact.Text = UserLogin.ToString();
        }        
    }

    protected void dtListContacts_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (_customer != null)
        {
            Contact contact = _customer.Contacts[e.Item.ItemIndex];
            if (e.CommandName == "EditContact")
            {
                if (contact != null)
                {
                    Session["EditContact"] = contact;
                    Response.Redirect("editContact.aspx?c=" + contact.Id);
                }
            }
            else if (e.CommandName == "DeleteContact")
            {
                if (contact != null && _customer != null && _customer.Contacts.Count > 0)
                {
                    _customerService.DeleteContactOfAccount(contact.Id);
                    UserLogin.Customer.Contacts.RemoveAt(e.Item.ItemIndex);
                    Response.Redirect("Default.aspx");
                }
            }
        }
    }
}